package com.gopay.checkorder.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.service.BankOrderService;
import com.gopay.checkorder.service.task.BankCountDownLatch;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.PabConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.dayend.BaseDayendEntry;
import com.gopay.common.domain.bank.pab.ebank.PabB2cMessage;
import com.gopay.common.domain.bank.pab.ebank.PabQueryOrdersMessageRes;
import com.gopay.common.domain.cashier.BankCardType;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.domain.cps.vo.InAccInfo;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.BankInteractRemoteService;

public class CheckOrderPabB2cImpl implements BankOrderService {
	private final static Logger logger = LoggerFactory.getLogger("PAB");

	@Resource(name = "bankClient")
	private BankInteractRemoteService bankClient;

	@Override
	public CheckOrderResult checkOrder(BankOrder bankOrder, String target) {
		CheckOrderResult checkOrderResult = new CheckOrderResult();
		checkOrderResult.setSuccess(false);
		checkOrderResult.setOrderId(bankOrder.getOrderId());
		try {

			PabB2cMessage base = new PabB2cMessage();
			base.setOrderId(bankOrder.getOrderId());
			base.setBankOrderId(bankOrder.getOutOrderId());
			base.setOrderDate(DateUtils.format(bankOrder.getSendBankTm(),"yyyyMMddHHmmss"));
			base.setBank(BankConstants.BANK_LOGO_PAB);
			base.setBankIntrChann(BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode()));
			base.setBankBusProd(BisBankBusinessProd.get(bankOrder.getBankBizProdCode()));
			Result result = bankClient.query(base);
System.out.println("=============Result:" + ToStringBuilder.reflectionToString(result));
			if (result == null || !result.isSuccess()) {
				logger.error("bankClient fail.");
				logger.error(ToStringBuilder.reflectionToString(result,ToStringStyle.DEFAULT_STYLE));
				
				if(result ==null || ProcCodeConstants.PROC_CODE_100E6037.equals(result.getResultCode())){
					BankCountDownLatch.failIncrease(bankOrder.getBankCode());
				}
			
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
				checkOrderResult.setErrMsg("系统返回对账结果为空");
				return checkOrderResult;
			}
			// 获取订单清单交易的申请数据的数据类
			PabQueryOrdersMessageRes response = null;
			response = (PabQueryOrdersMessageRes) result.getDefaultModel();
			if (response == null) {
				logger.error("fail:orderId:" + bankOrder.getOrderId()+ " errcode:" + ProcCodeConstants.PROC_CODE_100E6022+ " err:bis query is null.");
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
				checkOrderResult.setErrMsg("银行返回对账结果为空");
				return checkOrderResult;
			}
		
			String commitFlag = response.getStatus();// 1：成功2：失败3: 支付结果未知
			// 成功处理
			if (PabConstants.TRAN_SDB_DEAL_CHECK_IN_NEW.equals(commitFlag)) {
				logger.error("平安银行B2C订单: " + bankOrder.getOrderId()+ "交易返回成功，结账标记：" + commitFlag + "，执行账务处理");
				checkOrderResult.setSuccess(true);
				InAccInfo accInfo = new InAccInfo();
				accInfo.setOrderId(bankOrder.getOrderId());
				accInfo.setAmount(response.getOrderAmount());
				accInfo.setRetCode("00");
				accInfo.setCardType(BankCardType.Unknown.toString());
				checkOrderResult.setAccInfo(accInfo);
			} else if(PabConstants.TRAN_SDB_DEAL_CHECKOUT_NEW.equals(commitFlag)){
				logger.error("平安银行B2C订单: " + bankOrder.getOrderId()
						+ "交易银行结账标记：" + commitFlag + "，失败，暂不进行账务处理");
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
				checkOrderResult.setErrMsg(PabConstants.TRAN_SDB_MESSAGE_DEAL_CHECKOUT_NEW);
			}else if(PabConstants.TRAN_SDB_DEAL_DONT_CHECK_NEW.equals(commitFlag)){
				logger.error("平安银行B2C订单: " + bankOrder.getOrderId()
						+ "交易银行结账标记：" + commitFlag + "，交易结果未知，暂不进行账务处理");
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
				checkOrderResult.setErrMsg(PabConstants.TRAN_SDB_MESSAGE_DEAL_DONT_CHECK_NEW);
			}
			else {
				logger.error("平安银行B2C订单: " + bankOrder.getOrderId()
						+ "交易银行结账标记：" + commitFlag + "，未知，暂不进行账务处理");
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
				checkOrderResult.setErrMsg("对账成功，暂不进行账务处理");
			}
			System.out.println("=============checkOrderResult:" + ToStringBuilder.reflectionToString(checkOrderResult));
			return checkOrderResult;
		} catch (Exception e) {
			logger.error("pab b2c bankorder fail.", e);
			checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
			checkOrderResult.setErrMsg("银行对账异常");
			return checkOrderResult;
		}

	}
	@Override
	public BankOrder getDbOrder() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public BaseMessage dayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<CheckOrderResult> checkDayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Result getDayendMsg(String string) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public CheckOrderResult checkDayendOrder(BaseDayendEntry bdm, BankOrder bo) {
		// TODO Auto-generated method stub
		return null;
	}
}
